package com.wanmait.hotelManage.webservlet;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.wanmait.hotelManage.dao.UserinfoDAO;
import com.wanmait.hotelManage.vo.City;
import com.wanmait.hotelManage.vo.Userinfo;

/**
 * Servlet implementation class UserinfoServlet
 */
@WebServlet("/UserinfoServlet")
@MultipartConfig
public class UserinfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public UserinfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String action=request.getParameter("action");	
	if("login".equals(action)) {
		this.login(request, response);
	}else if("register".equals(action)) {
		this.register(request, response);
	}
	else if("upduserinfo".equals(action)) {
		this.upduserinfo(request, response);
	}
	else if("upduserpassword".equals(action)) {
		this.upduserpassword(request, response);
	}else if("drop".equals(action)) {
		this.drop(request, response);
	}	
	}
	protected void drop(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		request.getSession().removeAttribute("userinfo");
		response.sendRedirect(request.getContextPath()+"");
	}
	protected void upduserpassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{	
		Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
		String userId=request.getParameter("id");
		String newPassword=request.getParameter("newpassword");
		userinfo.setPassword(newPassword);
		UserinfoDAO.getUserinfodao().updpassword(newPassword,Integer.parseInt(userId) );
		request.getSession().setAttribute("userinfo", userinfo);
		response.sendRedirect(request.getContextPath()+"/login.jsp");
	}
	protected void upduserinfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
		
		String userid=request.getParameter("id");
		String username=request.getParameter("username");
		String userTelePhone=request.getParameter("usertetelephone");
		
		String userCity=request.getParameter("city");
		if(userid==null) {
			userid=userinfo.getId()+"";
		}
		else if(username==null) {
			username=userinfo.getUsername();
		}
		else if(userTelePhone==null) {
			userTelePhone=userinfo.getTelephoneNumber();
		}
		else if(userCity==null) {
			userCity=userinfo.getCity().getName();
		}
		int userCityId=UserinfoDAO.getUserinfodao().findCity(userCity);
		
		/**
 
    if(fileext!="") {  
      String path=request.getServletContext().getRealPath("/static/images");
      String fileName = UUID.randomUUID().toString();
      fengpart.write(path+"/"+fileName+fileext);
      fengpart.delete();
      roomType.setPic(fileName+fileext);
      RoomTypeDAO.getRoomtypedao().update(roomType);
    }else {
      RoomTypeDAO.getRoomtypedao().noImageUpdate(roomType);
    }
		 */
		Part part = request.getPart("userpic");
		
		String head = part.getHeader("content-disposition");
		int i = head.lastIndexOf(".");
		String fileext = "";
		
		try {
		      fileext = head.substring(i, head.length()-1);
		    } catch (Exception e) {
		      
		    }
		
		if(fileext!="") {  
		      String path=request.getServletContext().getRealPath("/static/images/touxiang");
		      String fileName = UUID.randomUUID().toString();
		      part.write(path+"/"+fileName+fileext);
		      part.delete();
		      //roomType.setPic(fileName+fileext);
		      //RoomTypeDAO.getRoomtypedao().update(roomType);
		    UserinfoDAO.getUserinfodao().upduserinfo(username,userTelePhone, fileName+fileext, userCityId,Integer.parseInt(userid) );
		   Userinfo userinfos=UserinfoDAO.getUserinfodao().FinfUserinfo(userinfo.getId());
		    request.getSession().setAttribute("userinfo", userinfos);
		      response.sendRedirect(request.getContextPath()+"/FrontDesk/center/profile.jsp");
		    }else {
		    	
		    	UserinfoDAO.getUserinfodao().upduserinfo(username,userTelePhone,  userCityId,Integer.parseInt(userid) );
		    	 Userinfo userinfos=UserinfoDAO.getUserinfodao().FinfUserinfo(userinfo.getId());
				    request.getSession().setAttribute("userinfo", userinfos);
		    	response.sendRedirect(request.getContextPath()+"/FrontDesk/center/profile.jsp");
		    	
		      //RoomTypeDAO.getRoomtypedao().noImageUpdate(roomType);
		    }
		
	
		
	}
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
		HttpSession session = request.getSession();
		request.setCharacterEncoding("utf-8");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
	    String code=request.getParameter("code");
	    String rightCode=(String)session.getAttribute("code");
		Userinfo userinfo=UserinfoDAO.getUserinfodao().getUserinfodao().findByUserinfoUsernameAndpassword(username, password);
		
		if(code==null||!code.equals(rightCode)) {
			response.sendRedirect(request.getContextPath()+"/login.jsp?msg=code");	
			return;
		}
		if(userinfo==null) {
			response.sendRedirect(request.getContextPath()+"/login.jsp?msg=error");
		}
		else {
			session.setAttribute("userinfo", userinfo);
			String url1 = (String)session.getAttribute("goUrl1");
			if(url1==null||"http://localhost:8080/hotelManage/login.jsp?msg=error".equals(url1)||"http://localhost:8080/hotelManage/login.jsp".equals(url1)) {
				response.sendRedirect(request.getContextPath()+"/indexServlet?action=hot");
			}else {
				
				response.sendRedirect(url1.toString());
			}
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	 protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   	    String username=request.getParameter("username");
	    	String password=request.getParameter("password");
	    	HttpSession session=request.getSession();
	    	String rightCode=(String)session.getAttribute("code");
	    	
		       if(UserinfoDAO.getUserinfodao().exsitUsername(username))
		       {
		    	  response.sendRedirect(request.getContextPath()+"/reg.jsp?msg=username"); 
		    	  return;
		       }
		       else
		       {
		    	 Userinfo userinfo=new Userinfo();
		    	 userinfo.setUsername(username);
		    	 userinfo.setPassword(password);
		    	 //调用DAO添加方法
		    	 UserinfoDAO.getUserinfodao().insert(userinfo);
		    	 //重新定向到登录页面
		    	 response.sendRedirect(request.getContextPath()+"/login.jsp");
       }


}
}
